﻿import QtQuick 2.0
import QtQuick.Controls 2.14


// 显示消息对话框，对话框会自动定时消失
Popup{
    id: dialogShowMsg
    //visible: false
    anchors.centerIn: parent
    focus: true
    modal: false
    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

    // 显示内容属性
    property string strShowMsg: ""//qsTr("弹出消息")
    // 自动关闭超时时间
    property int autoCloseTimeout: 5000
    // 文本颜色
    property string textColor: "#FF168bd7"
    
    // 显示消息函数
    function showMsgDialog(msg, closeTimeout = 5000, msgColor = "#FF168bd7")
    {
        //console.log("showMsgDialog:" + msg)
        close();// 先关闭
        strShowMsg = msg;
        autoCloseTimeout = closeTimeout;
        textColor = msgColor;
        anchors.centerIn = parent;  // 每次都要设置否则不会居中
        open();// 打开显示
    }
    contentItem:Label{
        text: dialogShowMsg.strShowMsg
        anchors.fill: parent
        wrapMode: Text.WrapAnywhere
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        color: textColor
        opacity: 1.0
        //font.family: customFont.name
        font.pixelSize: 15
        MouseArea{// 鼠标区域
            anchors.fill: parent
            onClicked: {
                dialogShowMsg.close();
            }
        }
        PropertyAnimation on opacity {
            running: dialogShowMsg.visible
            //property: "opacity"
            from: 1.0
            to: 0
            easing.type: Easing.InQuad
            duration: dialogShowMsg.autoCloseTimeout
        }
        /*ColorAnimation on color {// 逐渐消失动画
            running: dialogShowMsg.visible
            from: "#FF168bd7"
            to: "#00168bd7"
            easing.type: Easing.InQuad
            duration: dialogShowMsg.autoCloseTimeout
        }*/
    }
    background:Rectangle{// 去掉背景边框
        anchors.fill: parent
        color: "#F0CCCCCC"
        border.width: 0
        radius: 20
        
        PropertyAnimation on opacity {
            running: dialogShowMsg.visible
            //property: "opacity"
            from: 1.0
            to: 0
            //easing.type: Easing.InQuad
            duration: dialogShowMsg.autoCloseTimeout
        }
        /*ColorAnimation on color {// 逐渐消失动画
            running: dialogShowMsg.visible
            from: "#F0CCCCCC"
            to: "#00CCCCCC"
            duration: dialogShowMsg.autoCloseTimeout
        }*/
    }
    Timer{// 定时器
        id: dialogShowMsgCloseTimer
        interval: dialogShowMsg.autoCloseTimeout
        running: false
        repeat: false
        onTriggered:{
            dialogShowMsg.close();
            //console.log("dialogShowMsgCloseTimer onTriggered")
        }
    }
    onOpened: {// 弹窗已打开
        //console.log("dialogShowMsg opened");
        dialogShowMsgCloseTimer.restart();
    }
    onClosed: {// 弹窗已关闭
        //console.log("dialogShowMsg closed")
        dialogShowMsgCloseTimer.stop();
    }
}
